package com.shangcheng.server.controller;

import com.shangcheng.model.domain.Admin;
import com.shangcheng.model.domain.UmsRole;
import com.shangcheng.model.vo.AdminVo;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.interceptor.UserHolder;
import com.shangcheng.server.service.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class UmsAdminController {

    @Autowired
    private UmsAdminService umsAdminService;


    //用户注册
    @PostMapping("register")
    public ResponseEntity register(@RequestBody Admin admin) {
        //将admin信息存入数据库,并返回结果,这里的token从responseHeaders里获取
        Admin register = umsAdminService.register(admin);
        return ResponseEntity.ok(new Result(200,register,"注册成功"));
    }

    //登录
    @PostMapping("login")
    public Result login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        Map retMap = umsAdminService.login(username, password);
        return new Result(200, retMap, "操作成功");
    }

    //登出
    @PostMapping("logout")
    public Result logout() {
        UserHolder.remove();
        return new Result(200, null, "操作成功");
    }

    //获取当前登录者用户信息
    @GetMapping("info")
    public Result info(String name) {
        String username = UserHolder.getUserName();
        AdminVo adminVo = umsAdminService.findUser(username);
        return new Result(200, adminVo, "操作成功");
    }


    //删除指定的用户信息
    @PostMapping("delete/{id}")
    public ResponseEntity delete(@PathVariable("id") Integer id) {
        int row = umsAdminService.deleteById(id);
        return ResponseEntity.ok(new Result(200,row,""));
    }

    //根据用户名或姓名分页获取用户列表
    @GetMapping("list")
    public Result list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       String keyword) {
        PageResult pr = umsAdminService.list(pageNum, pageSize, keyword);
        return new Result(200, pr, "操作成功");
    }

    //获取指定用户的角色
    @GetMapping("role/{adminId}")
    public Result getRole(@PathVariable("adminId") Integer adminId) {
        List<UmsRole> roles = umsAdminService.getRole(adminId);
        //todo 不显示下拉框中的角色,并且显示的是id不是name
        return new Result(200, roles, "操作成功");
    }

    //给用户分配角色
    @PostMapping("role/{update}")
    public Result update(@RequestParam("adminId") Integer adminId,
                         @RequestParam("roleIds") List<Integer> roleIds) {
        int row = umsAdminService.update(adminId, roleIds);
        return new Result(200, row, "操作成功");
    }

    //获取指定用户的信息
    @GetMapping("admin/{id}")
    public Result getMsg(@PathVariable("id")Integer id){
        Admin admin=umsAdminService.getMsg(id);
        return new Result(200,admin,"");
    }

    //修改指定用户的信息
    @PostMapping("update/{id}")
    public Result updateById(@PathVariable("id") Integer id , @RequestBody Admin admin ){
        int row=umsAdminService.updateById(id,admin);
        return new Result(200,row,"");
    }

    //修改指定用户的密码
    @PostMapping("updatePassword")
    public Result updatePwd(@RequestBody Map map) {
        int row=umsAdminService.updatepwd(map);
        return new Result(200,row,"");
    }

    //修改账号状态
    @PostMapping("updateStatus/{id}")
    public Result updateStatus(@PathVariable("id") Integer id,@RequestParam Integer status){
        int row=umsAdminService.updateStatus(id,status);
        return new Result(200,row,"");
    }
}
